<template>
  <!-- 智慧工地平台-资料管理 -->
  <popup :show="dataShow" title="智慧工地平台-资料管理" @onClone="dataFun">
    <div class="dataDialog">
      <section class="buttonBox">
        <ul>
          <li
            v-for="(item, index) in buttonList"
            :key="index"
            :class="buttonIndex == index ? 'active' : ''"
            @click="
              buttonIndex = index;
              getFileList();
            "
          >
            {{ item.name }}
          </li>
        </ul>
        <ul>
          <li
            v-for="(item, index) in ThreeMonths"
            :key="index"
            :class="dateIndex == index ? 'active' : ''"
            @click="
              dateIndex = index;
              getFileList();
            "
          >
            {{ item }}
          </li>
        </ul>
      </section>
      <section class="titleBox">
        <el-table
          :data="tableData"
          style="width: 100%; height:50px,margin-top: 20px,border: 1px solid #495A79,"
          :row-style="{ height: 50 + 'px' }"
          :header-cell-style="{
            background: '#104584',
            color: '#fff',
            borderColor: '#495A79',
            height: 50 + 'px',
            padding: 0 + 'px',
          }"
          :cell-style="{
            background: '#0C2451',
            color: '#fff',
            height: 50 + 'px',
            borderColor: '#495A79',
            padding: 6 + 'px',
          }"
        >
          <el-table-column prop="name" label="文件名"> </el-table-column>
          <el-table-column prop="createUser" label="上传人"> </el-table-column>
          <el-table-column prop="id" label="大小"> </el-table-column>
          <el-table-column prop="createTime" label="上传时间">
          </el-table-column>
          <el-table-column label="操作" width="100" height="50">
            <template slot-scope="scope">
              <!-- <a target='_black' :href='http://view.officeapps.live.com/op/view.aspx?src=scope.row.showUrl'>查看</a> -->
              <el-button
                @click="toView(scope.row)"
                type="text"
                size="small"
                v-if="scope.row.showUrl"
                >查看</el-button
              >
              <el-button
                @click="downloadFun(scope.row)"
                type="text"
                size="small"
                v-if="scope.row.downloadUrl"
                >下载</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </section>
      <!-- <section class="pagination">
        <select v-model="selected" @change="selectChange">
          <option
            v-for="(option, idx) in options"
            :key="idx"
            :value="option.value"
          >
            {{ option.text }}
          </option>
        </select>
        <MoPaging
          :page-index="currentPage"
          :total="count"
          :page-size="pageSize"
          @pageChange="pageChange"
        >
        </MoPaging>
        <div class="pege">
          跳至
          <input type="tel" v-model="page" @blur="inputBlur" />
          页
        </div>
      </section> -->
    </div>
    <!-- <el-dialog
      title="预览"
      :visible.sync="viewVisible"
      width="100%"
      height="100%"
      :before-close="closeDialog"
    >
      <div class="pdf" v-show="fileType === 'pdf'">
        <pdf :src="pdfsrc"></pdf>
      </div>
    </el-dialog> -->
  </popup>
</template>

<script>
import popup from "../../popup.vue";
import MoPaging from "../mo-paging.vue";

import api from "@/util/api";
import * as dayjs from "dayjs";
export default {
  components: {
    popup,
    MoPaging,
  },
  name: "dataDialog",
  data() {
    return {
      dataDialog: `<div>1111111111</div>`,
      buttonList: [],
      buttonIndex: 0,
      ThreeMonths: [],
      dateIndex: 0,
      tableData: [],
      page: 1,
      pageSize: 7, //每页显示10条数据
      currentPage: 1, //当前页码
      count: 100, //总记录数,
      selected: 7,
      options: [
        { text: "5条/页", value: 5 },
        { text: "7条/页", value: 7 },
        { text: "9条/页", value: 9 },
      ],
      viewVisible: false, // 弹框隐藏
      fileType: "pdf", // 文件类型
      pdfsrc: "", // 文件地址
    };
  },
  mounted() {
    this.getAllFolders();
  },
  watch: {},
  computed: {
    dataShow: function () {
      return this.$store.state.dataShow;
    },
  },
  methods: {
    dataFun(data) {
      let that = this;
      that.$store.dispatch("changeFun", {
        str: "dataShow",
        cont: data,
      });
    },
    // 查看
    toView(row) {
      console.log("toView", row);
      if (!/\.(pdf|PDF)$/.test(row.name)) {
        window.open(
          "https://view.officeapps.live.com/op/view.aspx?src=" + row.showUrl,
          "_blank"
        );
        return false;
      } else {
        window.open( row.showUrl, "_blank");
      }
    },
    // 下载
    downloadFun(row) {
      console.log(row);
      window.open(row.downloadUrl);
    },
    closeDialog(done) {
      done();
    },
    //从page组件传递过来的当前page
    pageChange(page) {
      this.currentPage = page;
      this.page = page;
      this.getFileList();
    },
    // 每页条数改变
    selectChange() {
      this.pageSize = this.selected;
      this.getFileList();
    },
    // 当前页改变
    inputBlur() {
      // this.currentPage = this.page
      this.pageChange(+this.page);
    },

    /** API */
    // 获取近三个月时间
    getThreeMonths() {
      api.getThreeMonths({}, (res) => {
        res.data.data.map((item) => {
          this.ThreeMonths.push(dayjs(item).format("YYYY-MM"));
        });
        console.log(this.ThreeMonths);
        this.getFileList();
      });
    },
    // 大屏二级界面获取目录类型页签列表
    getAllFolders() {
      api.getAllFolders({}, (res) => {
        this.buttonList = res.data.data;
        this.getThreeMonths();
      });
    },
    // 弹框文件列表
    getFileList() {
      api.getFileList(
        {
          parentId: this.buttonList[this.buttonIndex].id,
          seachMonth: this.ThreeMonths[this.dateIndex],
          pageNum: this.currentPage,
          pageSize: this.pageSize,
        },
        (res) => {
          this.tableData = res.data.list;
          this.count = res.data.total;
        }
      );
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.dataDialog {
  width: 1200px;
  height: 680px;
  overflow: auto;
  .buttonBox {
    width: 100%;
    height: auto;
    padding-bottom: 30px;
    display: flex;
    justify-content: space-between;
    ul {
      display: flex;
      justify-content: space-between;
      color: #fff;
      li {
        list-style-type: none;
        width: 100px;
        height: 43px;
        background-color: #135ba7;
        border-radius: 4px;
        text-align: center;
        line-height: 43px;
        margin-left: 10px;
      }
      li:nth-child(1) {
        margin: 0;
      }
      .active {
        background-color: #ff891d;
      }
    }
  }
  .pagination {
    width: fit-content;
    margin: 0 auto;
    padding-top: 10px;
    select {
      width: 95px;
      height: 40px;
      border: 1px solid #e6ecf3;
      border-radius: 2px;
      margin-left: 12px;
      padding-left: 12px;
      line-height: 40px;
      font-size: 15px;
      text-align: center;
      color: #fff;
      overflow: hidden;
      background-color: #00000000;
      option {
        color: #000;
      }
    }
    .pege {
      display: inline-block;
      color: #fff;
      font-size: 15px;
      input {
        width: 63px;
        height: 40px;
        color: #fff;
        border: 1px solid #e6ecf3;
        border-radius: 2px;
        padding-left: 6px;
        background-color: #00000000;
        box-sizing: border-box;
      }
    }
  }
}
.el-table--border::after,
.el-table--group::after,
.el-table::before {
  background-color: #495a79 !important;
}
</style>

